<?php

namespace app\admin\controller;

use app\service\deviceService;
use app\service\roominfoService;
use think\facade\Db;
use think\facade\View;

class Staff extends Common
{

    public function index(){
        $com = $this->pidTokenId();
        $whe=array();
        $whe[] = ["community_id",'in',$com['community_id']];
        $communitylist=(new roominfoService())->communityList($whe);
        View::assign('communitylist',$communitylist);
        $page = request()->has('page') ? request()->get('page') : 1;

        $where=array();
        $where[] = ["community_id",'in',$com['com_id']];
        $export = $where;
        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);
        $pagelist = Db::name('staff')

            ->where($where)->order('id desc')->paginate(200, false, [
            'page'  =>  $page,
            'query' =>  $whe
        ]);;

        $number = Db::name('staff')
            ->where($whe)->count();
        if(!empty($number)){
            foreach($pagelist as $info){

                $list[] = $info;
                View::assign('list',$list);
            }
        }else{
            View::assign('list',$pagelist);
        }
        View::assign('number',$number);
        //View::assign("list",$pagelist);
        View::assign("pagelist",$pagelist);
        return View::fetch("index");
    }
    public function add(){
        $com = $this->pidTokenId();

        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //var_dump($data);exit;
            //验证表单信息
            $validate = $this->validate($data,[
                'name|名称' => 'require' ,
                'community_id|小区名称' => 'require' ,
                'mobile|手机号码' => 'require|alphaNum',
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            //判断账号名称是否重复
            $info = Db::name('staff')->where('mobile',$data['mobile'])->find();;
            if($info){
                $this->error('该手机号码已存在');
            }
            $community=Db::table("community")->where("id",$data["community_id"])->find();
            //将密码md5加密
            $data['password'] = md5(env("APP.STAFF_PASSWORD"));
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $data["community_name"]=$community["community_name"];
            $data["status"]="on";
            //将数据插入数据库
            $result =  Db::name('staff')->insertGetId($data);;
            //判断数据是否插入成功
            if($result){
                return "添加管理员成功";

            }else{
                $this->error('添加管理员失败',url('add'));
            }
        }else{
            $whe=array();
            $whe[] = ["community_id",'in',$com['community_id']];
            $community_list=(new roominfoService())->communityList($whe);
            View::assign('community_list',$community_list);
            return View::fetch('add');
        }
    }
    public function passwordedit(){
        //查询当前账号信息
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('用户不存在');
            }
            //查询管理员数据
            $info =  Db::table('staff')->where("id",$id)->find();
            View::assign('info',$info);
            return View::fetch('password');
        }else{
            $data = request()->post();
            $validate=  $this->validate($data,[
                'id'        =>  'require',
                'password|密码' => 'require',
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            $data['password'] = md5($data['password']);
            unset($data['repass']);
            //修改数据
            $result = Db::name('staff')->update($data);;
            if($result){
                return "密码修改成功";

            }else{
                return "密码修改失败";

            }
        }
    }
}